package com.ym.core.es.service;

import cn.hutool.core.util.IdUtil;
import com.ym.common.utils.StringUtil;
import com.ym.core.es.config.ElasticsearchIndexProperties;
import com.ym.core.es.domain.index.BaseIdIndex;
import com.ym.core.es.domain.index.GlobalSearchWordIndex;
import com.ym.core.es.engine.SearchQueryEngine;
import com.ym.core.es.query.QueryParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 全局关键词
 *
 * @author: 李涛
 * @version: 2020年01月07日 14:17
 */
@Service
@Slf4j
public class GlobalSearchWordIndexService implements ApplicationRunner {

	private static final Class index = GlobalSearchWordIndex.class;

	@Autowired
	private SearchQueryEngine searchQueryEngine;

	@Autowired
	private ElasticsearchIndexProperties setting;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		init();
	}

	/**
	 * 索引初始化
	 */
	public void init() {
		searchQueryEngine.deleteIndex(index);
		searchQueryEngine.createIndex(index, setting.getSetting());
	}

	/**
	 * 删除全部
	 */
	public void deleteAll() {
		searchQueryEngine.deleteIndex(index);
	}

	/**
	 * 删除搜索高亮
	 *
	 * @param dataType  数据类型
	 * @param tableName 表名
	 */
	public void deleteWords(String dataType, Class<?> tableName) {
		CriteriaQuery query = new CriteriaQuery(
				Criteria.where("dataType").is(dataType)
						.and("tableName").is(StringUtil.toUnderScoreCase(tableName.getSimpleName())
				)
		);
		searchQueryEngine.deleteIndexByQuery(index, query);
	}


	/**
	 * 删除搜索高亮
	 *
	 * @param dataType  数据类型
	 * @param tableName 表名
	 * @param id        相关ID
	 */
	public void deleteWord(String dataType, Class<?> tableName, Long id) {
		CriteriaQuery query = new CriteriaQuery(
				Criteria.where("dataType").is(dataType)
						.and("tableName").is(StringUtil.toUnderScoreCase(tableName.getSimpleName()))
						.and("busId").is(id)
		);
		searchQueryEngine.deleteIndexByQuery(index, query);
	}

	/**
	 * 删除搜索高亮
	 *
	 * @param dataType  数据类型
	 * @param tableName 表名
	 * @param ids        相关ID
	 */
	public void deleteWords(String dataType, Class<?> tableName, Long[] ids) {
		CriteriaQuery query = new CriteriaQuery(
				Criteria.where("dataType").is(dataType)
						.and("tableName").is(StringUtil.toUnderScoreCase(tableName.getSimpleName()))
						.and("busId").in(ids)
		);
		searchQueryEngine.deleteIndexByQuery(index, query);
	}

	/**
	 * 批量插入高亮关键词
	 *
	 * @param heightWordIndices
	 */
	public void insertBatch(List<GlobalSearchWordIndex> heightWordIndices) {
		searchQueryEngine.saveOrUpdate(heightWordIndices);
	}

	/**
	 * 构建
	 *
	 * @param tableName
	 * @param dataTypeKey
	 * @param data        基础数据
	 * @param fieldName
	 * @param fieldValue
	 * @return
	 */
	public GlobalSearchWordIndex buildGlobalSearchWordIndex(String tableName, String dataTypeKey, BaseIdIndex data, String fieldName, String fieldValue) {
		GlobalSearchWordIndex globalSearchWordIndex = new GlobalSearchWordIndex();
		globalSearchWordIndex.setId(IdUtil.fastSimpleUUID());
		globalSearchWordIndex.setBusId(Long.valueOf(data.getId()));
		globalSearchWordIndex.setDataType(dataTypeKey);
		globalSearchWordIndex.setTableName(tableName);
		globalSearchWordIndex.setFieldName(fieldName);
		globalSearchWordIndex.setFieldValue(fieldValue);
		globalSearchWordIndex.setProv(data.getProv());
		globalSearchWordIndex.setCity(data.getCity());
		globalSearchWordIndex.setArea(data.getArea());
		globalSearchWordIndex.setPublishStatus(data.getPublishStatus());
		return globalSearchWordIndex;
	}

	/**
	 * 查询方法
	 *
	 * @param queryParser
	 * @return
	 */
	public List<GlobalSearchWordIndex> queryForList(QueryParser queryParser) {
		return searchQueryEngine.queryForList(queryParser, index);
	}
}
